﻿@namespace MXERP.Blazor.Components
@inherits MudComponentBase
@typeparam T


<tr class="md-table-row" @key="Item">
    @foreach (var column in Table.RenderedColumns)
    {
        @Cell(column, Item)
    }
</tr>
@if (Expanded)
{
    @if (Children != null && Children.Count() > 0)
    {
        @Table.Body(Children, Item, this.Level + 1)
    }

}

@code {
    internal RenderFragment Cell(Column<T> column, T item)
    {
        return
            @<text>
            @{
                var cell = new Cell<T>(this, column);
            }
            <td data-label="@column.Title" class="@cell.ClassName" style="@column.CellStyle">
                <div class="d-flex align-center">
                    @if(Table.ShowExpandedColumn?.uid == column.uid)
                    {
                        <span class="md-table-row-indent indent-level-@Level" style="padding-left: @($"{(Level * column.Table.IndentSize)}px");"></span>
                        if(Children != null && Children.Count() > 0)
                        {
                            <MudIconButton Class="md-table-row-`"
                                           Size="Size.Small"
                                           Icon="@(Expanded ? Icons.Material.Filled.ExpandMore : Icons.Material.Filled.ChevronRight)"
                                           OnClick="@(() => SetExpanded(!Expanded))" />
                        }
                    }
                    @if(column.Ellipsis)
                    {
                        <Ellipsis>
                            @Cell(column,cell)
                        </Ellipsis>
                    }
                    else
                    {
                        @Cell(column,cell)
                    }
                </div>
            </td>
            </text>;
    }

    internal RenderFragment Cell(Column<T> column, Cell<T> cell)
    {
        return
            @<text>
            @if (column.CellTemplate is not null)
            {
                @column.CellTemplate(cell._cellContext)
            }
            else
            {
                @cell.ComputedValue
            }
            </text>;
    }
}
